<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sortId">
              <!--              <j-category-select v-model="model.sortId" pcode="id" placeholder="请选择所属分类" back="name"  />-->
              <j-multi-select-tag v-model="model.sortId" placeholder="请选择" dict-code="hl_service_sort,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" rows="4" placeholder="请输入内容" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload v-model="model.cover"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input v-model="model.amount" placeholder="请输入金额"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile">
              <a-input v-model="model.mobile" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否首页展示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isIndex">
              <j-dict-select-tag type="list" v-model="model.isIndex" dictCode="yn" placeholder="请选择手否首页展示" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上下架" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isShow">
              <j-dict-select-tag type="list" v-model="model.isShow" dictCode="yn" placeholder="请选择上下架" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'HlServiceOfficialForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        title: [
          { required: true, message: '请输入标题!' },
        ],
        content: [
          { required: true, message: '请输入内容!' },
        ],
        cover: [
          { required: true, message: '请输入封面!' },
        ],
        imgs: [
          { required: true, message: '请输入详情图!' },
        ],
        amount: [
          { required: true, message: '请输入金额!' },
          { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' },
        ],
        isShow: [
          { required: true, message: '请输入上下架!' },
        ],
      },
      url: {
        // add: "/huole/hlServiceOfficial/add",
        add: "/test/ServiceOffcial/add",
        edit: "/huole/hlServiceOfficial/edit",
        queryById: "/huole/hlServiceOfficial/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            consloe.log(res)
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    handleCategoryChange(value, backObj) {
      this.model = Object.assign(this.model, backObj);
    }
  }
}
</script>